package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.BuyHouseListAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.BuyHuseBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

public class BuyHouseListActivity extends BasePersonActivity {

    @BindView(R.id.recycle_activity_buy_house_list)
    YRecycleview mYRecycleview;
    private View mBuyHouseListView;
    private BuyHouseListAdapter mBuyHouseListAdapter;
    private LoadingPagerHead mLoadingPagerHead;

    List<BuyHuseBean> buyHuseBeanList=new ArrayList<>();
    private int currentPage=1;

    @Override
    protected View getContentView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }

            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(BuyHouseListActivity.this, R.layout.activity_buy_house_list);
            }

            @Override
            protected void reLoading() {
                retry();

            }
        };



      //  mBuyHouseListView = UIUtils.inflate(context, R.layout.activity_buy_house_list);
        return mLoadingPagerHead;
    }

    /**
     * 重新加载
     */
    private void retry() {
        Observable<StatusCode<List<BuyHuseBean>>> buyHouseListStatusCodeObservable = ApiUtils.getApi().getBuyHouseList(BaseApplication.sUserInfoBean.userId, Contans.rows, "1",BaseApplication.sUserInfoBean.getToken());
        HttpUtil.getInstance().toSubscribe(buyHouseListStatusCodeObservable, new ProgressSubscriber<List<BuyHuseBean>>(this) {

            @Override
            protected void _onNext(StatusCode<List<BuyHuseBean>> t) {
                List<BuyHuseBean> listStatusCode = t.getData();
                if (listStatusCode!=null &&listStatusCode.size()>0){
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                    buyHuseBeanList.clear();
                    buyHuseBeanList.addAll(listStatusCode);
                    mBuyHouseListAdapter.notifyDataSetChanged();

                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {

                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                buyHuseBeanList.clear();
                mBuyHouseListAdapter.notifyDataSetChanged();


            }
        },"",lifecycleSubject,false,true);

    }

    @Override
    protected void initData() {
        super.initData();
        mTvTitle.setText("购房清单");
        buyHuseBeanList.clear();
        mBuyHouseListAdapter = new BuyHouseListAdapter(this,buyHuseBeanList);
        //设置布局管理器
        mYRecycleview.setLayoutManager(new LinearLayoutManager(this));
        //设置adapter
        mYRecycleview.setAdapter(mBuyHouseListAdapter);
        //设置Item增加、移除动画
        mYRecycleview.setItemAnimator(new DefaultItemAnimator());
        //添加分割线
     //   mYRecycleview.addItemDecoration(new DividerItemDecoration(
        //this, DividerItemDecoration.VERTICAL));


            retry();
    }


    public static void startActivity(Context context){
        Intent intent=new Intent(context,BuyHouseListActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mYRecycleview.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                Observable<StatusCode<List<BuyHuseBean>>> buyHouseListCodeObservable = ApiUtils.getApi().getBuyHouseList(BaseApplication.getUserId(),  Contans.rows, "1",BaseApplication.sUserInfoBean.getToken());
                HttpUtil.getInstance().toSubscribe(buyHouseListCodeObservable, new ProgressSubscriber<List<BuyHuseBean>>(BuyHouseListActivity.this) {


                    @Override
                    protected void _onNext(StatusCode<List<BuyHuseBean>> t) {
                        List<BuyHuseBean> buyHuseBeans = t.getData();

                        if (buyHuseBeans!=null && buyHuseBeans.size()>0){
                            buyHuseBeanList.clear();
                            buyHuseBeanList.addAll(buyHuseBeans);
                            ToastUtils.makeText("刷新成功");
                            currentPage=1;
                        }else {
                            ToastUtils.makeText("刷新失败");
                        }
                        mYRecycleview.setReFreshComplete();
                        mBuyHouseListAdapter.notifyDataSetChanged();


                    }

                    @Override
                    protected void _onError(String message) {


                        ToastUtils.makeText(message);
                        mYRecycleview.setReFreshComplete();
                        mBuyHouseListAdapter.notifyDataSetChanged();

                    }
                },"",lifecycleSubject,false,true);



            }

            @Override
            public void onLoadMore() {
                currentPage++;
                Observable<StatusCode<List<BuyHuseBean>>> buyHouseListCodeObservable = ApiUtils.getApi().getBuyHouseList(BaseApplication.getUserId(),  Contans.rows, currentPage+"",BaseApplication.sUserInfoBean.getToken());
                HttpUtil.getInstance().toSubscribe(buyHouseListCodeObservable, new ProgressSubscriber<List<BuyHuseBean>>(BuyHouseListActivity.this) {

                    @Override
                    protected void _onNext(StatusCode<List<BuyHuseBean>> t) {

                        List<BuyHuseBean> buyHuseBeans = t.getData();
                        if (buyHuseBeans!=null && buyHuseBeans.size()>0){
                            buyHuseBeanList.addAll(buyHuseBeans);
                            mYRecycleview.setloadMoreComplete();
                            mBuyHouseListAdapter.notifyDataSetChanged();
                        }else {

                            ToastUtils.makeText("已经加载全部数据");
                            currentPage--;
                        }
                        mYRecycleview.setloadMoreComplete();
                        mBuyHouseListAdapter.notifyDataSetChanged();

                    }

                    @Override
                    protected void _onError(String message) {


                        ToastUtils.makeText(message);
                        mYRecycleview.setloadMoreComplete();
                        mBuyHouseListAdapter.notifyDataSetChanged();

                    }
                },"",lifecycleSubject,false,true);

            }
        });
    }

    @Override
    protected void returnMainActivity(View v) {
        MainActivity.startActivity(this);


    }
}
